package com.jumi.microservice.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.entity.OrderLogistics;
import org.apache.ibatis.annotations.*;
import org.springframework.scheduling.annotation.Async;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author by mazhao
 * @Classname OrderLogisticsMapper
 * @Description
 * @Date 2020/7/29 11:39
 */
@Mapper
public interface OrderLogisticsMapper extends BaseMapper<OrderLogistics> {
    /**
     * 批量插入
     *
     * @param list
     * @return
     */
    @Insert("<script>INSERT INTO order_logistics "
            + "(logistics_no,warehouse,goods_label,is_alone,self_flag,receiver_phone,receiver_address,receiver_info,spilt_merge_status,receiver_name,logistics_status,is_group)"
            + "VALUES "
            + "<foreach collection='list' item='o' index='index' separator=',' >"
            + "(#{o.logisticsNo},#{o.warehouse},#{o.goodsLabel},#{o.isAlone},#{o.selfFlag},#{o.receiverPhone},#{o.receiverAddress},#{o.receiverInfo},#{o.spiltMergeStatus},#{o.receiverName},#{o.logisticsStatus},#{o.isGroup})" +
            "</foreach></script>")
    int batchInsertLogistics(List<OrderLogistics> list);

    /**
     * 批量更新删除标识
     *
     * @param logisticsNoList
     * @return
     */
    @Update("<script> update order_logistics set del_flag = 1 where logistics_no in(" +
            "<foreach  collection = 'list' item = 'p' index = 'index'  separator= ',' >" +
            "#{p}" +
            "</foreach>) and logistics_status = 0 and del_flag = 0</script>")
    int batchUpdateDel(List<String> logisticsNoList);

    /**
     * 批量更新是否确认拆合标识
     * @param isSpiltMergeConfirm
     * @param list
     * @return
     */
    @Update("<script> update order_logistics set is_spilt_merge_confirm = #{isSpiltMergeConfirm} " +
            "where del_flag = 0 and logistics_no in(" +
            "<foreach  collection = 'list' item = 'p' index = 'index'  separator= ',' >" +
            "#{p}" +
            "</foreach>)</script>")
    int batchUpdateIsSpiltMergeConfirm(@Param("list")List<String> list,@Param("isSpiltMergeConfirm") Byte isSpiltMergeConfirm);

    /**
     * 统计
     *
     * @param benchmarkTime
     * @param goodsName
     * @param orderNos
     * @param page
     * @return
     */
    @Select("<script> " +
            "select count(*) as count,real_warehouse as warehouse,concat(receiver_phone,receiver_address) as receiverInfo,receiver_name as receiverName," +
            "receiver_phone as receiverPhone,receiver_address as receiverAddress" +
            " from logistics_goods a where" +
            " is_wdt_push = 0 and del_flag = 0 and logistics_status = 0 and logistics_step = 1 and self_flag = 1 and create_time &lt;= #{benchmarkTime}" +
            " <if test='goodsName != null and goodsName.trim()!=&quot;&quot;'>" +
            " and goods_name LIKE CONCAT('%',#{goodsName},'%')" +
            " </if>" +
            " <if test='orderNos != null and orderNos.trim()!=&quot;&quot;'>" +
            " and order_no in( ${orderNos} )" +
            " </if>" +
            " <if test='receiverName != null and receiverName.trim()!=&quot;&quot;'>" +
            " and receiver_name = #{receiverName}" +
            " </if>" +
            " <if test='receiverPhone != null and receiverPhone.trim()!=&quot;&quot;'>" +
            " and receiver_phone = #{receiverPhone}" +
            " </if>" +
            " GROUP BY real_warehouse,receiverInfo" +
            " order by count desc" +
            "</script>")
    IPage<Map<String, Object>> getReceiverInfoByPackageCount(@Param("benchmarkTime") Date benchmarkTime,
                                                             @Param("goodsName") String goodsName,
                                                             @Param("orderNos") String orderNos,
                                                             @Param("receiverName") String receiverName,
                                                             @Param("receiverPhone") String receiverPhone,
                                                             Page<Map<String, Object>> page);


    /**
     * 批量更新实发仓库
     *
     * @param list
     * @return
     */
    @Update("<script>UPDATE order_logistics SET" +
            " warehouse = CASE id" +
            "<foreach collection='list' item='l' index='index'>" +
            " WHEN #{l.id} THEN #{l.warehouse}" +
            "</foreach>" +
            "    END" +
            " WHERE id IN (" +
            "<foreach collection='list' item='l' index='index' separator=',' >" +
            "#{l.id}" +
            "</foreach>)</script>")
    int batchUpdateRealWarehouse(List<OrderLogistics> list);

    /**
     * 批量更新包裹拆合标识
     *
     * @param list
     * @return
     */
    @Update("<script>UPDATE order_logistics SET" +
            " spilt_merge_status = 1" +
            " WHERE id IN (" +
            "<foreach collection='list' item='l' index='index' separator=',' >" +
            "#{l.id}" +
            "</foreach>)</script>")
    int batchUpdateSpiltMergeStatusById(List<OrderLogistics> list);

    /**
     * 批量更新包裹拆合标识
     *
     * @param list
     * @return
     */
    @Update("<script>UPDATE order_logistics SET" +
            " spilt_merge_status = 2" +
            " WHERE logistics_no IN (" +
            "<foreach collection='list' item='l' index='index' separator=',' >" +
            "#{l}" +
            "</foreach>)</script>")
    int batchUpdateSpiltMergeStatusByLogisticsNo(List<String> list);

    /**
     * 批量更新包裹状态
     *
     * @param list
     * @return
     */
    @Update("<script>UPDATE order_logistics SET" +
            " logistics_status = CASE logistics_no" +
            "<foreach collection='list' item='l' index='index'>" +
            " WHEN #{l.logisticsNo} THEN #{l.logisticsStatus}" +
            "</foreach>" +
            "    END" +
            " WHERE logistics_no IN (" +
            "<foreach collection='list' item='l' index='index' separator=',' >" +
            "#{l.logisticsNo}" +
            "</foreach>) and del_flag = 0 and logistics_status = 0</script>")
    int batchUpdateLogisticsStatus(List<OrderLogistics> list);

}
